/* MOS                                                        */
/* Giordano Mion, Luca David Opromolla and Alessandro Sforza  */
/* University of Sussex, Banco de Portugal & NC State, and Bologna       */

program drop _all
clear all
clear matrix
capture log close
set more off
local path = "/Users/lucadavidopromolla/Projects/MOS_rev"
cd "`path'/computation"
log using "`path'/logs/MOS_sumstat_Apr_11_2022_rev.log", replace
set mem 12g
set matsize 7500

// Firm-level: entry rates
use data_ang,clear
keep if ano>=1998 & ano<=2005
gen ymkt_spec_exp = 1 if mkt_spec_exp>0 & mkt_spec_exp!=.
gen nmkt_spec_exp = 1 if mkt_spec_exp==0
gen yentry = entry if ymkt_spec_exp==1
gen nentry = entry if nmkt_spec_exp==1
collapse (count) empresa (sum) ymkt_spec_exp nmkt_spec_exp entry yentry nentry,by(ano)
gen smkt_spec_exp = ymkt_spec_exp/empresa
gen erate = entry/empresa
gen yerate = yentry/ymkt_spec_exp
gen nerate = nentry/nmkt_spec_exp
l ano empresa *mkt_spec_exp *entry
l ano smkt_spec_exp *erate
twoway (line erate ano) || (line yerate ano) || (line nerate ano)
graph export "`path'/results/graphs/ds_entry.eps", as(eps) fontface(Times) replace
graph export "`path'/results/graphs/ds_entry.pdf", as(pdf) fontface(Times) replace
gen bef2002 = ano<2002
collapse (sum) empresa ymkt_spec_exp nmkt_spec_exp entry yentry nentry,by(bef2002)
gen smkt_spec_exp = ymkt_spec_exp/empresa
gen erate = entry/empresa
gen yerate = yentry/ymkt_spec_exp
gen nerate = nentry/nmkt_spec_exp
l bef2002 empresa *mkt_spec_exp *entry
l bef2002 smkt_spec_exp *erate


use data_ang_prmk,clear
foreach var of varlist mkt_spec_exp prgr_spec_exp prmk_spec_exp {
use data_ang_prmk,clear
keep if ano>=1998 & ano<=2005
gen y`var' = 1 if `var'>0 & `var'!=.
gen n`var' = 1 if `var'==0
gen yentry = entry if y`var'==1
gen nentry = entry if n`var'==1
collapse (count) empresa (sum) y`var' n`var' entry yentry nentry,by(ano)
gen s`var' = y`var'/empresa
gen erate = entry/empresa
gen yerate = yentry/y`var'
gen nerate = nentry/n`var'
l ano empresa *`var' *entry
l ano s`var' *erate
twoway (line erate ano) || (line yerate ano) || (line nerate ano)
graph export "`path'/results/graphs/ds_entry_`var'.eps", as(eps) fontface(Times) replace
graph export "`path'/results/graphs/ds_entry_`var'.pdf", as(pdf) fontface(Times) replace
}


// Sectoral break-down exports
use firm_regressions_exp_gr,clear
keep if ano>=1998 & ano<=2005
collapse (sum) v_est_pr_gr,by(pr_gr ano)
egen double texp = sum(v_est_pr_gr),by(ano)
replace v_est_pr_gr = v_est_pr_gr/texp
table pr_gr ano,stat(mean v_est) nformat(%12.2f)

// Summary stats firm-level vars, firm-destination-year sample
use ang,clear
keep if dest==8
sort empresa dest ano
gen entry = (de_==1 & de_[_n-1]==0 & empresa==empresa[_n-1] & dest==dest[_n-1] & other_exporter==0) // Keep only period in which the firm can enter into a new destination
sort empresa dest ano
keep if (de_[_n-1]==0 & empresa==empresa[_n-1] & dest==dest[_n-1] & other_exporter==0)
keep if ano>=1998 & ano<=2005
replace mkt_spec_exp = 1 if mkt_spec_exp>0 & mkt_spec_exp!=.
local ctr_exp_mf = "relsize relapp_lbprod ln_firmage foreign sh_skilled sh_skilled_d relmean_age relsd_age relmean_educ relsd_educ"
sum entry mkt_spec_exp `ctr_exp_mf' if ano==2005

// Number of firms exporting to Angola
use MMT_trade_status_entryb_basic,clear
keep if dest==8
collapse (sum) de_,by(ano)
l

// Number of managers with experience in Angola


log close
